package controllers;

import java.net.URL;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Optional;
import java.util.ResourceBundle;

import Dao.DBConnect;
import application.Main;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import models.CarProductModel;
import models.OrderModel;
import models.ShopModel;
import models.UserModel;

import javax.jws.soap.SOAPBinding;

public class AdminController  implements Initializable  {


//	public Label lb_u_01;
	// Declare DB objects
	DBConnect conn = null;
	Statement stmt = null;
	UserModel um = null;
	ShopModel sp = null;


	@FXML
	private Button btn_u_add;
	@FXML
	private Button btn_u_edit;

//    @FXML
//    private Button btn_p_del;



	@FXML
	private VBox vbox_m_u;





	CarProductModel cpm;
	OrderModel od;

	/***** TABLEVIEW intel *********************************************************************/


	@FXML
	private HBox hb01;
	@FXML
	private Button btnDel;
	@FXML
	private HBox hb02;
	@FXML
	private TableView<UserModel> tblUser;
	@FXML
	private TableColumn<UserModel, String> uname;
//	@FXML
//	private TableColumn<UserModel, String> passwd;
	@FXML
	private TableColumn<UserModel, Integer> uid;
	@FXML
	private TableColumn<UserModel, Integer> role;


	@FXML
	private VBox vbox_m_s;
	@FXML
	private TableView<ShopModel> tblShop;
	@FXML
	private TableColumn<ShopModel, Integer> sid;
	@FXML
	private TableColumn<ShopModel, String> shop_name;
	@FXML
	private TableColumn<ShopModel, String> shop_address;
	@FXML
	private TableColumn<ShopModel, String> shop_tel;


	@FXML
	private TableView<OrderModel> order_view02;
	@FXML
	private TableColumn<OrderModel, Integer> oid2;
	@FXML
	private TableColumn<OrderModel, String> product_name_o2;
	@FXML
	private TableColumn<OrderModel, String> order_status2;
	@FXML
	private TableColumn<OrderModel, Integer> pur_num2;

	@FXML
	private Label lbinfo;

	@FXML
	private Label lb_u_01;
	@FXML
	private TextField txt_u_01;
	@FXML
	private TextField txt_u_02;
	@FXML
	private ChoiceBox cb_u_01;


	@FXML
	private TextField txt_s_01;
	@FXML
	private TextField txt_s_02;
	@FXML
	private TextField txt_s_03;


	ButtonType foo = new ButtonType("OK", ButtonBar.ButtonData.OK_DONE);
	ButtonType bar = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
	Alert a = new Alert(Alert.AlertType.NONE,"",foo,bar);
	Alert b = new Alert(Alert.AlertType.CONFIRMATION,"",foo);


	public void initialize(URL location, ResourceBundle resources) {

		uid.setCellValueFactory(new PropertyValueFactory<UserModel, Integer>("id"));
		uname.setCellValueFactory(new PropertyValueFactory<UserModel, String>("uname"));
//		passwd.setCellValueFactory(new PropertyValueFactory<UserModel, String>("passwd"));
		role.setCellValueFactory(new PropertyValueFactory<UserModel, Integer>("role"));
		uid.setStyle( "-fx-alignment: CENTER;");
		uname.setStyle( "-fx-alignment: CENTER;");
		role.setStyle( "-fx-alignment: CENTER;");
		tblUser.setVisible(Boolean.FALSE);
		cb_u_01.getItems().addAll("0","1","2");
		// auto adjust width of columns depending on their content

//		tblProduct2.setColumnResizePolicy((param) -> true);
//		Platform.runLater(() -> customResize(tblProduct2));
		sid.setCellValueFactory(new PropertyValueFactory<ShopModel, Integer>("sid"));
		shop_name.setCellValueFactory(new PropertyValueFactory<ShopModel, String>("shopName"));
		shop_address.setCellValueFactory(new PropertyValueFactory<ShopModel, String>("shopAddr"));
		shop_tel.setCellValueFactory(new PropertyValueFactory<ShopModel, String>("shopTel"));
		tblShop.setVisible(Boolean.FALSE);
		sid.setStyle( "-fx-alignment: CENTER;");
		shop_name.setStyle( "-fx-alignment: CENTER;");
		shop_address.setStyle( "-fx-alignment: CENTER;");
		shop_tel.setStyle( "-fx-alignment: CENTER;");
//		hb01.setVisible(Boolean.FALSE);
//		hb02.setVisible(Boolean.FALSE);
		vbox_m_s.setVisible(Boolean.FALSE);

		oid2.setCellValueFactory(new PropertyValueFactory<OrderModel, Integer>("id"));
		product_name_o2.setCellValueFactory(new PropertyValueFactory<OrderModel, String>("product_name"));
		order_status2.setCellValueFactory(new PropertyValueFactory<OrderModel, String>("order_status"));
		pur_num2.setCellValueFactory(new PropertyValueFactory<OrderModel, Integer>("p_num"));
		order_view02.setVisible(Boolean.FALSE);
		oid2.setStyle( "-fx-alignment: CENTER;");
		product_name_o2.setStyle( "-fx-alignment: CENTER;");
		order_status2.setStyle( "-fx-alignment: CENTER;");
		pur_num2.setStyle( "-fx-alignment: CENTER;");
		btn_u_add.setDisable(true);
		btn_u_edit.setDisable(true);









	}


	public AdminController() {

		um = new UserModel();
		sp = new ShopModel();
	}

	public void viewAccounts() {

		System.out.println("mouse click");
		btn_u_add.setDisable(false);
		btn_u_edit.setDisable(false);


		UserModel um1 = tblUser.getSelectionModel().getSelectedItem();
		System.out.println(um1.toString());
		lb_u_01.setText(String.valueOf(um1.getId()));
		txt_u_01.setText(um1.getUname());
		txt_u_02.setText(String.valueOf(um1.getRole()));

		cb_u_01.setValue(String.valueOf(um1.getRole()));
	}




    public void manageUser(ActionEvent actionEvent) {

		System.out.println("press button 1");
		vbox_m_u.setVisible(Boolean.TRUE);
		tblUser.setVisible(Boolean.TRUE);
		order_view02.setVisible(Boolean.FALSE);
		btn_u_add.setDisable(false);
		vbox_m_s.setVisible(Boolean.FALSE);
		tblShop.setVisible(Boolean.FALSE);
		tblUser.getItems().setAll(um.getAll());
//		lbinfo.setText("Welcome " + uname + " now you are viewing the product");
//		btn_u_add.setDisable(false);
    }


	public void manageShop(ActionEvent actionEvent) {

		System.out.println("in view shops");
		vbox_m_u.setVisible(Boolean.FALSE);
//		order_view01.setVisible(Boolean.FALSE);
		order_view02.setVisible(Boolean.FALSE);
//		hb01.setVisible(Boolean.FALSE);
//		hb02.setVisible(Boolean.FALSE);
		vbox_m_s.setVisible(Boolean.TRUE);
		tblShop.setVisible(Boolean.TRUE);
		tblShop.getItems().setAll(sp.getAll());
//		lbinfo.setText("Welcome " + uname + " now you are viewing the product");
		btn_u_add.setDisable(false);
	}

	public void logout(ActionEvent actionEvent) {

		try {
			AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("/views/LoginView.fxml"));
			Scene scene = new Scene(root);
			scene.getStylesheets().add(getClass().getResource("/application/styles.css").toExternalForm());
			Main.stage.setScene(scene);
			Main.stage.setTitle("Login");

		} catch (Exception e) {
			System.out.println("Error occured while inflating view: " + e);
		}
	}

	public void insertUser(MouseEvent mouseEvent) {

		System.out.println(txt_u_01.getText() + "  " +txt_u_02.getText());
		UserModel um2 = new UserModel();
		um2.setUname(txt_u_01.getText());
		um2.setPasswd(txt_u_02.getText());
		um2.setRole(Integer.parseInt(cb_u_01.getValue().toString()));
		a.setAlertType(Alert.AlertType.CONFIRMATION);
		a.setContentText("Do you want to add new user?");
		a.setHeaderText("");

		Optional<ButtonType> result = a.showAndWait();

		if (result.get() == foo) {
			if (um.checkuser(um2)) {
				b.setContentText("User exist");
				b.setHeaderText("");
				b.showAndWait();
			}
			else {

			um.insertRecord(um2);
	//			cpm.insertRecord(userid, Integer.parseInt(result.get()),cpms1.getProId());
			System.out.println(um2.toString() );
			b.setContentText("User add successful");
			b.setHeaderText("");
			b.showAndWait();
			tblUser.getItems().setAll(um.getAll());}
		}
	}



	public void resetPass(MouseEvent mouseEvent) {

		System.out.println(txt_u_01.getText() + "  " +txt_u_02.getText());
		UserModel um1 = tblUser.getSelectionModel().getSelectedItem();
		System.out.println(um1.toString());
		System.out.println(txt_u_01.getText());
		um1.setUname(txt_u_01.getText());
		um1.setPasswd(txt_u_02.getText());
		um1.setRole(Integer.parseInt(cb_u_01.getValue().toString()));
		a.setAlertType(Alert.AlertType.CONFIRMATION);
		a.setContentText("Do you want to reset the password?");
		a.setHeaderText("");

		Optional<ButtonType> result = a.showAndWait();

		if (result.get() == foo) {
			um.resetPass(um1);
			b.setContentText("Password reset successful");
			b.setHeaderText("");
			b.showAndWait();
			tblUser.getItems().setAll(um.getAll());
			btn_u_edit.setDisable(true);
		}
//			cpm.insertRecord(userid, Integer.parseInt(result.get()),cpms1.getProId());
//        System.out.println(cpm.toString() );

	}


	public void insertShop(MouseEvent mouseEvent) {

		System.out.println(txt_s_01.getText() + "  " +txt_s_02.getText());
		ShopModel sp1 = new ShopModel();
		sp1.setShopName(txt_s_01.getText());
		sp1.setShopAddr(txt_s_02.getText());
		sp1.setShopTel(txt_s_03.getText());
		a.setAlertType(Alert.AlertType.CONFIRMATION);
		a.setContentText("Do you want to add new Shop?");
		a.setHeaderText("");

		Optional<ButtonType> result = a.showAndWait();

		if (result.get() == foo) {
			sp.insertRecord(sp1);
			//			cpm.insertRecord(userid, Integer.parseInt(result.get()),cpms1.getProId());
			System.out.println(sp1.toString() );
			b.setContentText("New Shop add successful");
			b.setHeaderText("");
			b.showAndWait();
			tblShop.getItems().setAll(sp.getAll());}
	}
}
